package com.cmy.framework_base.basics

import android.app.Dialog
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.cmy.framework.resources.R

/**
 * @name:
 *  BaseDialogFragment
 * @package:
 *  com.cmy.framework_base.base
 * @date:
 *  2023/2/2217:14
 * @author
 *    cmy
 * @description
 *  基础弹窗
 */
open class BasicsDialogFragment : DialogFragment() {
    companion object{
        private const val ARG_PARAM_STYLE = "dialogStyle"
        private const val ARG_ROOT_RESOURCE_ID = "rootResourceId"

        fun newInstance(dialogStyle:Int,resId:Int ) : BasicsDialogFragment{
          val dialog = BasicsDialogFragment()
          val args = Bundle()
          args.putInt(ARG_PARAM_STYLE, dialogStyle)
          args.putInt(ARG_ROOT_RESOURCE_ID, resId)
          dialog.arguments = args
           return dialog
      }

        fun newInstance(resId:Int): BasicsDialogFragment{
            return newInstance(R.style.CustomDialog,resId)
        }
    }

    /**
     * 页面布局
     */
    private var  contentView: View? = null

    /**
     * 页面资源ID
     */
    private var resId :Int = 0

    /**
     * 页面资源配置属性
     */
    private var dialogStyle : Int = 0

    /**
     * 取消按钮是否屏蔽
     */
    private var isCanceledOnTouchOutSide = false


    private val mAnimResId: Int = R.style.AnimUpInDownOut

    /**
     * 弹窗基础
     */
    private var mDialog: Dialog? = null


    private var mWidth = 0
    private var mHeight = 0
    private val dialogWidthRatio = 0.0
    private val dialogHeightRatio = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            dialogStyle = arguments!!.getInt(ARG_PARAM_STYLE)
            resId = arguments!!.getInt(ARG_ROOT_RESOURCE_ID)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View ?{
        contentView = inflater.inflate(this.resId, container, false)
        return contentView
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setCanceledOnTouchOutSide(isCanceledOnTouchOutSide)
        dialog?.setCanceledOnTouchOutside(isCanceledOnTouchOutSide)

    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)
        mDialog = Dialog(requireContext(), dialogStyle)
        return mDialog!!
    }

    override fun onResume() {
        super.onResume()

        //设置参数
        val window = dialog!!.window
        val lp = window!!.attributes
        // 设置动画效果
        window.setWindowAnimations(this.mAnimResId)
        //设置宽度
        setWidthRatio(if (this.dialogWidthRatio == 0.0) 0.73 else this.dialogWidthRatio)
        lp.width = this.mWidth

        window.attributes = lp
    }
        /**
         * 设置宽度屏占比
         *
         * @param dialogWidthRatio
         */
        private fun setWidthRatio(dialogWidthRatio: Double) {
            val dm: DisplayMetrics = getDisplayMetrics()
            mWidth = (dm.widthPixels * dialogWidthRatio).toInt()
        }


        /**
         * 设置高度屏占比
         *
         * @param dialogHeightRatio
         */
        private fun setHeightRatio(dialogHeightRatio: Double) {
            val dm = getDisplayMetrics()
            mHeight = (dm.heightPixels * dialogHeightRatio).toInt()
        }
        /**
         * 获取宽高对象
         *
         * @return
         */
        private fun getDisplayMetrics(): DisplayMetrics {
            val windowManager = activity!!.window.windowManager
            val dm = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(dm)
            return dm
        }

    //------------------------公共方法-----------------------
    /**
     * 设置是否通过返回关闭弹窗
     */
    private fun setCanceledOnTouchOutSide(isCanceledOnTouchOutSide: Boolean) {
        this.isCanceledOnTouchOutSide = isCanceledOnTouchOutSide
    }


}
